/**
 * ====================================================================
 *
 *  Copyright 1999-2004 The Apache Software Foundation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * .
 *
 * [Additional notices, if required by prior licensing conditions]
 *
 * 
 * @author Christian
 * @since
 * @version 
 * @Date 16.04.2014
 */
package de.sisoft.gcjack.communication.asyncstask;

import android.util.Log;
import de.sisoft.gcjack.communication.ConnectionManager;
import de.sisoft.gcjack.exception.DefaultConnectionException;
import de.sisoft.gcjack.exception.NoResultFoundException;
import de.sisoft.gcjack.exception.UserAlreadyVotedException;
import de.sisoft.gcjack.model.AsyncResult;
import de.sisoft.gcjack.model.GeoCache;

/**
 *
 */
// params, progress, result
public class GetGeoCacheDetailsTask extends
	DefaultAsyncTask<String, Integer, AsyncResult> {

    // private AsyncResponse callingActivity;
    private ConnectionManager cnManager = new ConnectionManager();
    private GeoCache geocache = new GeoCache();

    private NoResultFoundException noResultException;
    private UserAlreadyVotedException userAlreadyVotedException;
    private DefaultConnectionException connectionException;

    /*
     * (non-Javadoc)
     * 
     * @see android.os.AsyncTask#onPreExecute()
     */
    @Override
    protected void onPreExecute() {
	super.onPreExecute();
	this.callingActivity.getActivity()
		.setProgressBarIndeterminateVisibility(true);
    }

    /*
     * (non-Javadoc)
     * 
     * @see android.os.AsyncTask#doInBackground(java.lang.Object[])
     */
    @Override
    protected AsyncResult doInBackground(String... params) {
	// TODO check if internet connectin is available

	geocache.setGcCode(params[0]);
	try {
	    geocache = cnManager.updateGeoCache(geocache);

	} catch (NoResultFoundException nrfe) {
	    Log.e("ERROR: ", nrfe.getMessage());
	    this.noResultException = nrfe;
	} catch (DefaultConnectionException dce) {
	    Log.e("ERROR: ", dce.getMessage());
	    this.connectionException = dce;
	} catch (UserAlreadyVotedException uave) {
		Log.e("ERROR: ", uave.getMessage());
		this.userAlreadyVotedException = uave;
	}

	return geocache;
    }

    /*
     * (non-Javadoc)
     * 
     * @see android.os.AsyncTask#onProgressUpdate(Progress[])
     */
    @Override
    protected void onProgressUpdate(Integer... progress) {
	super.onProgressUpdate(progress);
	// do some progress update stuff here
    }

    /*
     * (non-Javadoc)
     * 
     * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
     */
    @Override
    protected void onPostExecute(AsyncResult result) {

	this.callingActivity.getActivity()
		.setProgressBarIndeterminateVisibility(false);
	
	if (this.noResultException == null
		&& this.connectionException == null) {
	    callingActivity.processGeoCacheDetails(result);
	}

	if (this.noResultException != null) {
	    callingActivity.handleException(noResultException);
	    noResultException = null;
	}
	if (this.userAlreadyVotedException != null) {
	    callingActivity.handleException(userAlreadyVotedException);
	    userAlreadyVotedException = null;
	}

	// pDialog.dismiss();
	super.onPostExecute(result);
    }

    /*
     * (non-Javadoc)
     * 
     * @see android.os.AsyncTask#onCancelled(java.lang.Object)
     */
    @Override
    protected void onCancelled(AsyncResult result) {
	if (cnManager.getHttpGet() != null) {
	    cnManager.getHttpGet().abort();
	}
	if (cnManager.getHttpPost() != null) {
	    cnManager.getHttpPost().abort();
	}
	this.callingActivity.getActivity()
		.setProgressBarIndeterminateVisibility(false);
	super.onCancelled();
    }
}
